﻿using FastExecutor.Base.Annotation;
using FastExecutor.Base.Util;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.Base.Design
{
    [FastAuthAttribute]
    //[FastSQLInjectionAttribute]
    public class BaseController : Controller
    {
        [AutoWired]
        public DBUtil @DBUtil;
        public string UserGuid { set; get; }
        public string DisplayName { set; get; }
        public string LoginID { set; get; }
        public string Mobile { set; get; }
        public string EMail { set; get; }
        public string OUGuid { set; get; }
        public string OUName { set; get; }
        public string OUCode { set; get; }
        public string ParentOUGuid { set; get; }
        public JObject GetParams()
        {
            var req = Request;
            var postBody = string.Empty;
            JObject data = new JObject();
            if (req.HttpMethod == "POST")
            {
                var inp = req.InputStream;
                var sr = new StreamReader(inp);
                postBody = sr.ReadToEnd();
                //重置stream，且不要调用dispose,否则controller获取参数的时候获取不到
                inp.Seek(0, SeekOrigin.Begin);
                data = (JObject)JsonConvert.DeserializeObject(postBody);

            }
            else
            {
                foreach (string key in req.Params.AllKeys)
                {
                    data.Add(key, req.Params.GetValues(key)[0]);
                }
            }

            return data;
        }

    }
}
